package com.nuoniu.sibanyun.controller.finance;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.VInOutDto;
import com.nuoniu.sibanyun.entity.inOut.VAccountingField;
import com.nuoniu.sibanyun.service.inOut.IVAccountingFieldService;
import com.nuoniu.sibanyun.service.inOut.IVInOutRecordMainService;
import com.nuoniu.sibanyun.service.po.IVPurchaseInvoiceMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 存货核算
 * @auther 小啊刚
 * @date 2021-06-21
 */
@RestController
@Api(tags = "存货核算")
public class InventoryAccountingController {

    @Autowired
    private IVInOutRecordMainService inOutRecordMainService;
    @Autowired
    private IVPurchaseInvoiceMainService purchaseInvoiceMainService;
    @Autowired
    private IVAccountingFieldService accountingFieldService;

    @ApiOperation("获取采购/委外入库核算列表")
    @GetMapping("getPurchaseWarehousingList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountPeriod", value = "期间"),
            @ApiImplicitParam(name = "dataSource", value = "数据来源")
    })
    public HttpResult<VInOutDto> getPurchaseWarehousingList(Integer accountPeriod, String dataSource) {
        return HttpResult.ok(inOutRecordMainService.getAll(accountPeriod, dataSource));
    }

    @ApiOperation("获取自制入库核算列表")
    @GetMapping("getProductReceiveList")
    public HttpResult<VInOutDto> getProductReceiveList(Integer accountPeriod, String dataSource) {
        return HttpResult.ok(inOutRecordMainService.getProductReceiveList(accountPeriod, dataSource));
    }

    @ApiOperation("自制入库核算")
    @PostMapping("accountingProductReceiveList")
    public HttpResult accountingProductReceiveList(@RequestBody List<VInOutDto> inOutDtos) {
        inOutRecordMainService.accountingProductReceiveList(inOutDtos);
        return HttpResult.ok();
    }

    @ApiOperation("获取暂估核算列表")
    @GetMapping("getEvaluationList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountPeriod", value = "期间"),
            @ApiImplicitParam(name = "dataSource", value = "数据来源")
    })
    public HttpResult<VInOutDto> getEvaluationList(Integer accountPeriod, String dataSource) {
        if(StringUtils.isBlank(dataSource)) {
            throw new NuoNiuException("数据来源不能为空");
        }

        return HttpResult.ok(inOutRecordMainService.getEvaluationList(accountPeriod, dataSource));
    }

    @ApiOperation("保存暂估单")
    @PostMapping("saveEvaluationList")
    public HttpResult saveEvaluationList(@RequestBody List<VInOutDto> inOutDtos) {
        inOutRecordMainService.saveEvaluationList(inOutDtos);
        return HttpResult.ok();
    }

    @ApiOperation("获取外购发票列表")
    @GetMapping("getInvoiceList")
    public HttpResult getInvoiceList() {
        return HttpResult.ok(purchaseInvoiceMainService.getInvoiceList());
    }

    @ApiOperation("钩稽")
    @PostMapping("audit")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dataSource", value = "数据来源")
    })
    public HttpResult audit(@RequestBody Map<String, String> param) {
        if(param == null || StringUtils.isBlank(param.get("dataSource"))) {
            throw new NuoNiuException("数据来源不能为空");
        }

        inOutRecordMainService.audit(param.get("dataSource"));
        return HttpResult.ok();
    }

    @ApiOperation("采购/委外入库核算")
    @PostMapping("inventoryAccounting")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dataSource", value = "数据来源")
    })
    public HttpResult inventoryAccounting(@RequestBody Map<String, String> param) {
        if(param == null || StringUtils.isBlank(param.get("dataSource"))) {
            throw new NuoNiuException("数据来源不能为空");
        }

        inOutRecordMainService.inventoryAccounting(param.get("dataSource"));
        return HttpResult.ok();
    }

    @ApiOperation("获取材料/委外/产成品出库核算列表")
    @GetMapping("getOutInventoryAccountingList")
    public HttpResult getOutInventoryAccountingList(Integer accountPeriod, String dataSource) {
        return HttpResult.ok(inOutRecordMainService.getOutInventoryAccountingList(accountPeriod, dataSource));
    }

    @ApiOperation("材料出库核算")
    @PostMapping("outInventoryAccounting")
    public HttpResult outInventoryAccounting(Integer accountPeriod) {
        inOutRecordMainService.outInventoryAccounting(accountPeriod);
        return HttpResult.ok();
    }

    @ApiOperation("产成品出库核算")
    @PostMapping("productAccounting")
    public HttpResult productAccounting(Integer accountPeriod) {
        inOutRecordMainService.productAccounting(accountPeriod);
        return HttpResult.ok();
    }

    @ApiOperation("存货结账")
    @PostMapping("inventorySettleAccounts")
    public HttpResult inventorySettleAccounts(Integer year, Integer month, Integer status) {
        inOutRecordMainService.inventorySettleAccounts(year, month, status);
        return HttpResult.ok();
    }

    @ApiOperation("获取显示字段")
    @GetMapping("getAccountingField")
    public HttpResult getAccountingField(Integer type) {
        if(type == null) {
            throw new NuoNiuException("类型不能为空");
        }

        return HttpResult.ok(accountingFieldService.getAccountingField(type));
    }

    @ApiOperation("保存显示字段")
    @PostMapping("saveAccountingField")
    public HttpResult saveAccountingField(@RequestBody VAccountingField accountingField) {
        if(accountingField.getType() == null) {
            throw new NuoNiuException("类型不能为空");
        }

        accountingFieldService.saveAccountingField(accountingField);
        return HttpResult.ok();
    }
}
